/**
 * @copyright Copyright (c) 2025 CSG Digital Grid Technology (Guangdong) Co., Ltd.
 * @author Xie Jinhui
 */

/* 
 * Prevent recursive inclusion protection mechanism
 * This preprocessor directive ensures the header file contents are included only once
 * During compilation, prevents multiple definitions and redefinition errors
 * Standard practice for C/C++ header file organization and dependency management
 */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __CONFIG_H
#define __CONFIG_H

/* 
 * Include cJSON library for JSON parsing and generation
 * Provides data structures and functions for working with JSON format
 * Essential for configuration file parsing and manipulation operations
 */
#include "cJSON.h"

/* 
 * Include WWAN configuration definitions and constants
 * Provides cellular network specific definitions and parameter limits
 * Contains constants for APN names, route configurations, and interface settings
 */
#include "wwan_conf.h"

/* 
 * Include standard integer type definitions
 * Provides fixed-width integer types like uint8_t, uint32_t for consistent data representation
 * Ensures platform-independent integer sizing across different architectures
 */
#include <stdint.h>

/* 
 * Conditional compilation for configuration file path based on build type
 * DEBUG mode uses local development configuration file path
 * RELEASE mode uses system-wide production configuration file path
 * Enables different configuration management strategies for development vs deployment
 */
#ifdef DEBUG
/* 
 * Development configuration file path for testing and debugging
 * Local file path accessible during development without system permissions
 * Facilitates rapid testing and configuration changes during development cycle
 */
#define CONFIG_APN "sim_config.json"
#else
/* 
 * Production configuration file path for deployed systems
 * System directory path requiring appropriate file permissions
 * Ensures persistent configuration across system reboots in production environment
 */
#define CONFIG_APN "/etc/remotedial/sim_config.json"
#endif

/* 
 * Access Point Name (APN) set configuration structure definition
 * This structure organizes multiple APN configurations for a single carrier
 * Supports storage of APN credentials including name, username, and password
 * Maintains count of configured APNs for efficient iteration and management
 */
typedef struct
{
    /* 
     * Array of APN names for multiple APN configurations
     * Each entry stores a complete APN name string up to APN_NAME_LEN characters
     * Supports configuration of multiple APNs per carrier for redundancy
     */
    char apn[APN_NUM][APN_NAME_LEN];
    
    /* 
     * Array of username credentials corresponding to each APN
     * Stores authentication usernames for PPP connection establishment
     * Maintains alignment with APN array for credential pairing
     */
    char user[APN_NUM][APN_NAME_LEN];
    
    /* 
     * Array of password credentials corresponding to each APN
     * Stores authentication passwords for PPP connection security
     * Maintains alignment with APN and user arrays for complete credential sets
     */
    char password[APN_NUM][APN_NAME_LEN];

    /* 
     * Count of currently configured APNs in the arrays
     * Tracks the number of valid entries in apn, user, and password arrays
     * Used for bounds checking and iteration through configured APNs
     */
    int apn_num;

} apnSetTypeDef;

/* 
 * Complete network interface configuration structure definition
 * This structure contains all configuration parameters for a network interface
 * Includes default APN settings, carrier-specific APN sets, and routing information
 * Supports comprehensive network configuration management for cellular interfaces
 */
typedef struct
{
    /* 
     * Default APN configuration for the network interface
     * Primary APN name used when no carrier-specific configuration is available
     * Fallback configuration for general network connectivity
     */
    char apn[APN_NAME_LEN];
    
    /* 
     * Default username for APN authentication
     * Authentication credential for the default APN configuration
     * Used when establishing PPP connections with default settings
     */
    char user[APN_NAME_LEN];
    
    /* 
     * Default password for APN authentication
     * Security credential for the default APN configuration
     * Used in conjunction with default username for PPP authentication
     */
    char password[APN_NAME_LEN];

    /* 
     * Mobile carrier specific APN configuration set
     * Contains multiple APN configurations optimized for Mobile network operator
     * Supports carrier-specific optimizations and network configurations
     */
    apnSetTypeDef Mobile;
    
    /* 
     * Unicom carrier specific APN configuration set
     * Contains multiple APN configurations optimized for Unicom network operator
     * Supports carrier-specific network parameters and authentication methods
     */
    apnSetTypeDef Unicom;
    
    /* 
     * Telecom carrier specific APN configuration set
     * Contains multiple APN configurations optimized for Telecom network operator
     * Enables carrier-specific network configurations and service optimizations
     */
    apnSetTypeDef Telecom;

    /* 
     * Static IP route destination addresses
     * Array of IP addresses for configured static routes
     * Supports multiple route destinations for complex network topologies
     */
    char ip[ROUTE_IP_NUM][ROUTE_IP_LEN];
    
    /* 
     * Static IP route network masks
     * Array of subnet masks corresponding to static route destinations
     * Defines network scope for each configured static route
     */
    char mask[ROUTE_IP_NUM][ROUTE_IP_LEN];

    /* 
     * Count of configured static IP routes
     * Tracks the number of valid entries in ip and mask arrays
     * Used for route management and configuration validation
     */
    uint8_t route_cnt;

} configTypeDef;

/* 
 * Complete PPP daemon configuration structure definition
 * This structure contains configuration for all PPP interfaces and system defaults
 * Manages multiple network interfaces with their individual configurations
 * Provides system-wide default device selection for network connectivity
 */
typedef struct
{
    /* 
     * PPP0 interface configuration parameters
     * Contains complete network configuration for first PPP interface
     * Typically used for primary cellular modem connection
     */
    configTypeDef ppp0;
    
    /* 
     * PPP1 interface configuration parameters
     * Contains complete network configuration for second PPP interface
     * Typically used for secondary cellular modem connection or failover
     */
    configTypeDef ppp1;
    
    /* 
     * ETH1 interface configuration parameters
     * Contains complete network configuration for Ethernet interface
     * Supports wired network connectivity as backup or primary connection
     */
    configTypeDef eth1;

    /* 
     * Default network interface device selection
     * Specifies the preferred network interface for default routing
     * Used when multiple interfaces are available and priority must be established
     */
    char defaultDev[APN_NAME_LEN];

} pppdTypeDef;

/* 
 * APN management and temporary storage structure definition
 * This structure is used for APN configuration operations and temporary data storage
 * Supports both configuration modification and display functionality
 * Provides workspace for APN operations without affecting active configuration
 */
typedef struct
{
    /* 
     * Target network device for APN operations
     * Specifies which network interface (ppp0, ppp1) the APN operation applies to
     * Used to direct configuration changes to the appropriate interface
     */
    char dev[APN_NAME_LEN];
    
    /* 
     * Carrier name for carrier-specific APN operations
     * Identifies the mobile network operator (Mobile, Unicom, Telecom)
     * Determines which carrier-specific APN set to modify or display
     */
    char carrier_name[APN_NAME_LEN];
    
    /* 
     * Carrier operation flag for configuration type determination
     * Indicates whether operation applies to carrier-specific or default APN configuration
     * Controls configuration storage location and retrieval logic
     */
    int carrier_flag;

    // Temporary storage for APN addition operations
    /* 
     * Temporary APN name storage for configuration operations
     * Holds APN name during add/modify operations before permanent storage
     * Provides workspace for user input validation and processing
     */
    char apn[APN_NAME_LEN];
    
    /* 
     * Temporary username storage for configuration operations
     * Holds authentication username during APN configuration changes
     * Enables credential validation before committing to configuration
     */
    char user[APN_NAME_LEN];
    
    /* 
     * Temporary password storage for configuration operations
     * Holds authentication password during APN configuration changes
     * Supports secure credential handling during configuration updates
     */
    char password[APN_NAME_LEN];

    // Temporary storage for APN display operations
    /* 
     * Temporary PPP0 configuration storage for display operations
     * Holds complete PPP0 configuration data for user interface display
     * Enables configuration viewing without modifying active settings
     */
    configTypeDef ppp0;
    
    /* 
     * Temporary PPP1 configuration storage for display operations
     * Holds complete PPP1 configuration data for user interface display
     * Supports comprehensive configuration review across all interfaces
     */
    configTypeDef ppp1;

} apnTypeDef;

// External global variable declarations for configuration management
/* 
 * Global APN message structure for system-wide APN operations
 * This external variable provides shared access to APN configuration data
 * Used across multiple modules for consistent APN management
 * Maintains current APN operation state and temporary data storage
 */
// extern apnTypeDef apnMsg;
extern apnTypeDef apnMsg;

/* 
 * Global PPP daemon configuration structure for system-wide network settings
 * This external variable provides shared access to complete network configuration
 * Contains all interface configurations and system default settings
 * Serves as the primary configuration repository for the network subsystem
 */
extern pppdTypeDef pppdMsg;

/* 
 * External function declarations for configuration management operations
 * These functions provide configuration file handling and utility services
 * Support configuration persistence, parsing, and system integration
 */

/* 
 * Retrieve configuration data from specified file directory
 * This function reads configuration content from persistent storage
 * Supports both development and production configuration file paths
 * Provides error handling for file access and read operations
 * 
 * @param stringbuff Output buffer for configuration data storage
 * @param filedir Directory path containing configuration files
 * @return uint8_t Success indicator (TRUE/FALSE) for configuration retrieval
 */
extern uint8_t getConfig(uint8_t *stringbuff, const char *filedir);

/* 
 * Convert hexadecimal string representation to integer value
 * This utility function parses hexadecimal strings to numerical values
 * Supports both uppercase and lowercase hexadecimal digits
 * Used for configuration parameter conversion and validation
 * 
 * @param hex_str Null-terminated string containing hexadecimal representation
 * @return long Integer value equivalent of the hexadecimal input string
 */
extern long hex_to_int(char *hex_str);

/* 
 * Parse string data into cJSON object structure
 * This function converts JSON formatted strings into manipulatable cJSON objects
 * Provides JSON parsing capability for configuration file processing
 * Includes error detection for malformed JSON input data
 * 
 * @param str Input string containing JSON formatted data
 * @return cJSON* Pointer to parsed cJSON object structure, NULL on parsing failure
 */
extern cJSON *string2Json(uint8_t *str);

/* 
 * Write buffer content to specified filename
 * This function provides file output capability for configuration persistence
 * Supports configuration updates and backup operations
 * Includes error handling for file write operations and disk space issues
 * 
 * @param filename Target file path for data writing
 * @param buf Data buffer containing content to be written to file
 * @return int Success indicator (number of bytes written) or error code
 */
extern int write_file(const char *filename, const char *buf);

/* 
 * Retrieve current system logging status
 * This function queries the current state of system logging functionality
 * Used for conditional logging based on system configuration
 * Supports dynamic logging level management and control
 * 
 * @return uint8_t Current logging status indicator (enabled/disabled)
 */
extern uint8_t log_status_get(void);

/* 
 * End of header file inclusion protection
 * Closes the conditional compilation block started with #ifndef __CONFIG_H
 * Completes the recursive inclusion prevention mechanism
 */
#endif /* __CONFIG_H */